package com.example.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.ProductImages;
import com.example.mall.domain.ProductInventory;
import com.example.mall.dto.ProductImagesDto;
import com.example.mall.dto.ProductInventoryDto;
import com.example.mall.mapper.ProductImagesMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductImagesQuery;
import com.example.mall.query.ProductInventoryQuery;
import com.example.mall.service.IProductImagesService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.omg.CORBA.OMGVMCID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品图片表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class ProductImagesServiceImpl extends ServiceImpl<ProductImagesMapper, ProductImages> implements IProductImagesService {

    @Autowired
    private ProductImagesMapper productImagesMapper;
    /**
     * 根据商品ID查询商品图片列表
     * @param productId 商品ID
     */
    @Override
    public List<ProductImages> selectByProductId(Long productId) {
        AssertUtil.isNotNull(productId, "商品ID不能为空");
        LambdaQueryWrapper<ProductImages> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductImages::getProductId, productId);
        return this.list(queryWrapper);
    }

    @Override
    public PageDTO<ProductImagesDto> queryPage(ProductImagesQuery productImagesQuery) {
        if (productImagesQuery == null) {
            productImagesQuery = new ProductImagesQuery();
        }
        Page<ProductImages> page = productImagesQuery.toMpPageByDefaultCreateTimeAsc();
        Page<ProductImages> cartPage = lambdaQuery()
                .like(productImagesQuery.getProductId()!= null, ProductImages::getProductId, productImagesQuery.getProductId())
                .like(productImagesQuery.getImageId()!= null, ProductImages::getImageId, productImagesQuery.getImageId())
                .like(productImagesQuery.getImageUrl()!= null, ProductImages::getImageUrl, productImagesQuery.getImageUrl())
                .like(productImagesQuery.getIsMain()!= null, ProductImages::getIsMain, productImagesQuery.getIsMain())
                .orderByDesc(ProductImages::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, ProductImagesDto.class);
    }

    @Override
    public ProductImages queryProductImagesById(Long imageId) {
        AssertUtil.isNotNull(imageId, "图片ID不能为空");
        return productImagesMapper.selectById(imageId);
    }

    @Override
    public List<ProductImages> queryProductImagesList() {
        return this.list();
    }

    @Override
    public void insert(ProductImagesDto productImagesDto) {
        ProductImages productImages = new ProductImages();
        CopyUtils.copyProperties(productImagesDto, productImages);
        productImages.setImageId(CodeGenerateUtils.generateUniqueId());
        productImages.setCreateDate(new Date());
        productImages.setUpdateDate(new Date());
        productImagesMapper.insert(productImages);
    }

    @Override
    public void update(ProductImagesDto productImagesDto) {
        AssertUtil.isNotNull(productImagesDto.getImageId(), "图片ID不能为空");
        ProductImages productImages = productImagesMapper.selectById(productImagesDto.getImageId());
        AssertUtil.isNotNull(productImages, "未找到对应的图片记录");
        CopyUtils.copyProperties(productImagesDto, productImages);
        productImages.setUpdateDate(new Date());
        productImagesMapper.updateById(productImages);
    }

    @Override
    public void delete(Long imageId) {
        AssertUtil.isNotNull(imageId, "图片ID不能为空");
        ProductImages productImages = productImagesMapper.selectById(imageId);
        AssertUtil.isNotNull(productImages, "未找到对应的图片记录");
        productImagesMapper.deleteById(imageId);
    }

    @Override
    public void deleteBatch(List<Long> imageIds) {
        AssertUtil.isNotNull(imageIds, "图片ID列表不能为空");
        List<ProductImages> productImages = productImagesMapper.selectBatchIds(imageIds);
        AssertUtil.isNotNull(productImages, "未找到对应的图片记录");
        productImagesMapper.deleteBatchIds(imageIds);
    }
}
